<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/29
 * Time: 11:18
 */

defined('BASEPATH') OR exit('No direct script access allowed');

class Purchase extends Admin_Controller
{
    public function goods_list()
    {

        $this->load->model('purchase_goods_model');

        $this->load->library('pagination');
        $this->config->load('data_field');

        $purchase_goods = $this->config->item('purchase_goods');
        $assoc = $this->uri->uri_to_assoc();

        $_param_option = array(
            'goods_name' => array(
                'col' => 'col-lg-4',
                'type' => 'text',
                'placeholder' => '输入产品货号'
            ),
            'pg_status' => array(
                'col' => 'col-lg-2',
                'type' => 'option',
                'placeholder' => '选择状态',
                'option' => $purchase_goods['pg_status']
            )

        );

        $_param = array(
            'page' => $assoc['page'] > 0 ? intval($assoc['page']) - 1 : 0,
            'pg_status' => intval($this->input->get('pg_status')),
            'goods_name' => htmlspecialchars(trim($this->input->get('goods_name')))
        );

        $config['base_url'] = base_url('purchase/') . 'goods_list/page/';
        $config['total_rows'] = $this->purchase_goods_model->goods_num($_param);
        $config['num_links'] = 6;
        $config['per_page'] = $this->purchase_goods_model->limit_rows;
        $config['use_page_numbers']=true;
        $this->pagination->initialize($config);

        $_goods_list = $this->purchase_goods_model->goods_sku_list($_param);

        $data = array(
            'goods_list' => $_goods_list,
            'pages' => $this->pagination->create_links(),
            'param_option' => $_param_option,
            'param' => $_param
        );

        $this->load->view('purchase_goods_list', $data);
    }

    public function not_assign_goods_list()
    {
        $this->load->model('purchase_goods_model');
        $this->load->model('admin_model');
        $this->load->model('suppliers_model');

        $this->load->library('pagination');

        $assoc = $this->uri->uri_to_assoc();

        $_param_option = array(
            'goods_sn' => array(
                'col' => 'col-lg-3',
                'type' => 'text',
                'placeholder' => '填写货号...'
            ),
            'admin_id' => array(
                'col' => 'col-lg-2',
                'type' => 'option',
                'placeholder' => '选择采购人',
                'option' => $this->admin_model->admin_option()
            )
        );

        $_param = array(
            'admin_id' => $this->input->get('admin_id'),
            'goods_sn' => $this->input->get('goods_sn'),
            'supp_id' => $this->input->get('supp_id'),
            'page' => $assoc['page'] > 0 ? intval($assoc['page']) - 1 : 0,
            'pg_status' => 0
        );

        $config['base_url'] = base_url('purchase/') . 'not_assign_goods_list/page/';
        $config['total_rows'] = $this->purchase_goods_model->goods_num($_param);
        $config['num_links'] = 6;
        $config['per_page'] = $this->purchase_goods_model->limit_rows;
        $config['use_page_numbers']=true;
        $this->pagination->initialize($config);

        $_goods_list = $this->purchase_goods_model->goods_sku_list($_param);

        $_action_link = array(
            array('href' => '#myModal', 'name' => '生成采购批次')
        );

        $data = array(
            'goods_list' => $_goods_list,
            'pages' => $this->pagination->create_links(),
            'param_option' => $_param_option,
            'param' => $_param,
            'action_link' => $_action_link,
            'supp_list' => $this->suppliers_model->suppliers_option($_param['admin_id'])
        );

        $this->load->view('not_assign_goods_list', $data);
    }

    public function create_order()
    {
        $this->load->model('purchase_order_model');

        $admin_id = $this->input->post('admin_id');
        $supp_ids = $this->input->post('supp_id');
        $po_info = $this->input->post('po_info');

        foreach ($supp_ids as $supp_id) {

            $data = array(
                'admin_id' => $admin_id,
                'supp_id' => $supp_id,
                'po_info' => $po_info
            );
            $this->purchase_order_model->create_order($data);
        }

    }

    public function goods_log_list()
    {
        $this->load->model('purchase_goods_model');
        $this->load->library('pagination');

        $pg_id = $this->input->get('pg_id',0);


        die(json_encode($this->purchase_goods_model->goods_log_list($pg_id)));
    }

    public function order_list()
    {
        $this->load->model('purchase_order_model');
        $this->config->load('data_field');

        $this->load->library('pagination');

        $order = $this->config->item('purchase_order');
        $assoc = $this->uri->uri_to_assoc();

        $_param_option = array(
            'purchase_name' => array(
                'col' => 'col-lg-3',
                'type' => 'text',
                'placeholder' => '输入采购批次号、供应商',
            ),
            'po_status' => array(
                'col' => 'col-lg-2',
                'type' => 'option',
                'placeholder' => '选择站点',
                'option' => $order['po_status']
            )

        );

        $_param = array(
            'purchase_name' => htmlspecialchars(trim($this->input->get('purchase_name'))),
            'po_status' => intval($this->input->get('po_status')),
            'page' => $assoc['page'] > 0 ? intval($assoc['page']) - 1 : 0
        );

        $config['base_url'] = base_url('purchase/') . 'order_list/page/';
        $config['total_rows'] = $this->purchase_order_model->order_num($_param);
        $config['num_links'] = 6;
        $config['per_page'] = $this->purchase_order_model->limit_rows;
        $config['use_page_numbers']=true;
        $this->pagination->initialize($config);

        $_order_list = $this->purchase_order_model->order_list($_param);

        $data = array(
            'order_list' => $_order_list,
            'param_option' => $_param_option,
            'param' => $_param,
            'pages' => $this->pagination->create_links()
        );

        $this->load->view('purchase_order_list', $data);
    }

    public function order_info()
    {
        $this->load->model('purchase_goods_model');
        $this->load->model('purchase_order_model');
    
        $this->config->load('data_field');

        $assoc = $this->uri->uri_to_assoc();
    
        $status = $this->config->item('purchase_order');
        
        $purchase_order = $this->purchase_order_model->order_info(array('po_id' => $assoc['po_id']));
        
        $log_list = $this->purchase_order_model->log_list($purchase_order['po_id']);

        $data = array(
            'purchase_goods' => $this->purchase_goods_model->goods_list(array('po_id' => $assoc['po_id']), 1),
            'purchase_order' => $purchase_order,
            'log_list' => $log_list,
            'status' => $status
        );

        $this->load->view('purchase_order_info', $data);
    }
    
    public function log()
    {
        $this->load->model('purchase_goods_model');
        $this->load->model('purchase_order_model');
    
        $assoc = $this->uri->uri_to_assoc();
        $this->load->library('sys_message');
        
        $po_id = intval($assoc['po_id']);
        $info = htmlspecialchars(trim($this->input->post('info')));
        
        $purchase_order = $this->purchase_order_model->order_info(array('po_id' => $po_id));
    
        !$purchase_order && $this->sys_message->wrong('采购批次不存在!');
        
        if ($_POST) {
            
            $data = array(
                'info' => htmlspecialchars(trim($this->input->post('info'))),
                'po_id' => $purchase_order['po_id'],
                'po_status' => $purchase_order['po_status']
            );
    
            $handle_type = intval($this->input->post_get('handle_type'));
    
            switch ($handle_type) {
                case 1: //备注
    
                    !$data['info'] && $this->sys_message->wrong('请输入备注信息!');
                    
                    break;
                case 2: //批量设为采购中
    
                    //设置采购产品为采购中
                    $this->purchase_goods_model->set_po_status(array('po_id' => $po_id, 'pg_status' => 1), 2);
    
                    //检测批次是否设为采购中
                    $this->purchase_order_model->check_status($po_id, 2, $info);
                    
                    break;
                case 3: //单个产品设为采购中
    
                    $goods_id = intval($this->input->post('goods_id'));
                    !$goods_id && $this->sys_message->wrong('goods_id不存在!');
    
                    $this->purchase_goods_model->set_po_status(array('po_id' => $po_id, 'goods_id' => $goods_id, 'pg_status' => 1), 2);
    
                    $info .= ($info ? '<br /> ' : '') . "#{$goods_id} 设为采购中！";
                    
                    //检测批次是否设为采购中
                    $this->purchase_order_model->check_status($po_id, 2, $info);
                    
                    break;
            }
    
            $this->sys_message->success('成功!');
        }
    }

    public function operate()
    {
        $this->load->model('purchase_goods_model');
        $this->load->model('purchase_order_model');

        $assoc = $this->uri->uri_to_assoc();
        $this->load->library('sys_message');

        $po_id = intval($assoc['po_id']);
        !$po_id && $this->sys_message->wrong('po_id不存在!');
    
    
        $this->purchase_order_model->up_purchase_storage_list($po_id);
        
        switch ($assoc['step']) {
            case 'batch_purc_in':
                $this->purchase_goods_model->set_po_status(array('po_id' => $po_id, 'pg_status' => 1), 2);
                $this->purchase_order_model->check_status($po_id, 2);
                break;

            case 'purc_in':

                $goods_id = intval($assoc['goods_id']);
                !$goods_id && $this->sys_message->wrong('goods_id不存在!');

                $this->purchase_goods_model->set_po_status(array('po_id' => $po_id, 'goods_id' => $goods_id, 'pg_status' => 1), 2);
                $this->purchase_order_model->check_status($po_id, 2);
                break;

            case '':


                break;
        }

        $this->sys_message->success('成功!');
    }

    public function storage_list()
    {
        $this->load->model('suppliers_model');

        $this->load->library('pagination');

        $assoc = $this->uri->uri_to_assoc();

        $_param_option = array(
            'supp_name' => array(
                'col' => 'col-lg-3',
                'type' => 'text',
                'placeholder' => '填写供应商名称...'
            )
        );

        $_param = array(
            'supp_name' => trim($this->input->get('supp_name')),
            'page' => $assoc['page'] > 0 ? intval($assoc['page']) - 1 : 0
        );

        $config['base_url'] = base_url('purchase/') . 'storage_list/page/';
        $config['total_rows'] = $this->suppliers_model->supp_num($_param);
        $config['num_links'] = 6;
        $config['per_page'] = $this->suppliers_model->limit_rows;
        $config['use_page_numbers']=true;
        $this->pagination->initialize($config);

        $suppliers_list = $this->suppliers_model->suppliers_storage_list($_param);

        $data = array(
            'suppliers_list' => $suppliers_list,
            'param_option' => $_param_option,
            'param' => $_param,
            'pages' => $this->pagination->create_links()
        );

        $this->load->view('purchase_storage_list', $data);
    }
    
    public function storage_listing()
    {
        $this->load->model('suppliers_model');
        $this->load->model('purchase_storage_list_model');
        
        $this->load->library('pagination');
    
        $assoc = $this->uri->uri_to_assoc();
    
        $_param_option = array(
            'supp_name' => array(
                'col' => 'col-lg-3',
                'type' => 'text',
                'placeholder' => '填写供应商名称...'
            )
        );
    
        $purchase_storage_list = $this->purchase_storage_list_model->storage_list($assoc['supp_id']);
        
        $data = array(
            'purchase_storage_list' => $purchase_storage_list,
            'param_option' => $_param_option
        );
        
        $this->load->view('purchase_storage_listing', $data);
        
    }
    
    public function storage_listing_info()
    {
        $this->load->model('suppliers_model');
        $this->load->model('goods_sku_model');
        $this->load->model('purchase_storage_list_model');
    
        $assoc = $this->uri->uri_to_assoc();
    
        $supp_id = intval($assoc['psl_id']);
    
        $supp_info = $this->suppliers_model->suppliers_info(array('suppliers_id' => $supp_id));
    
        $goods_sku = $this->goods_sku_model->goods_psl_list($supp_id);
        
        $psl_info = $this->purchase_storage_list_model->storage_list_info($supp_id);
    
        $data = array(
            'supp_info' => $supp_info,
            'goods_sku' => $goods_sku,
            'psl_info' => $psl_info
        );
        
        $this->load->view('storage_listing_info', $data);
    }

    public function storage_info()
    {
        $this->load->model('suppliers_model');
        $this->load->model('goods_sku_model');
        $this->load->model('shelves_model');

        $assoc = $this->uri->uri_to_assoc();

        $supp_id = intval($assoc['supp_id']);

        $supp_info = $this->suppliers_model->suppliers_info(array('suppliers_id' => $supp_id));

        $goods_sku = $this->goods_sku_model->goods_supp_list($supp_id);
        $shelves = $this->shelves_model->shelves_list();

        $data = array(
            'supp_info' => $supp_info,
            'goods_sku' => $goods_sku,
            'shelves' => $shelves
        );

        $this->load->view('purchase_storage_info', $data);

    }
    
    public function print_code()
    {
        
        $this->load->model('goods_sku_model');
        $this->load->library('tcpdf_api');
    
        $assoc = $this->uri->uri_to_assoc();
    
        $sku_id = intval($assoc['sku_id']);
        $num = intval($assoc['num']);
        
        $goods_sku = $this->goods_sku_model->sku_info(array('sku_id' => $sku_id));
    
        $this->tcpdf_api->label($goods_sku['sku_sn'], $num);
    }

    public function in_storage()
    {

        $this->load->model('purchase_goods_model');
        $this->load->model('purchase_order_model');
        $this->load->model('goods_sku_model');
        $this->load->model('order_goods_model');
        $this->load->model('order_model');
        $this->load->model('shelves_model');

        $assoc = $this->uri->uri_to_assoc();

        $this->load->library('sys_message');

        $data['sgs_id'] = intval($assoc['sgs_id']);
        $sku_id = intval($assoc['sku_id']);
        $supp_id = intval($assoc['supp_id']);

        $goods_sku_storage = $this->goods_sku_model->goods_sku_storage_info(array(
            'sgs_id' => $data['sgs_id'],
            'sku_id' => $sku_id,
            'supp_id' => $supp_id
        ));
        !$goods_sku_storage && $this->sys_message->wrong('参数错误!');


        if ($_POST) {

            $data['admin_id'] = 1;
            $data['sku_id'] = intval($goods_sku_storage['sku_id']);
            $data['supp_id'] = intval($goods_sku_storage['supp_id']);
            $psl_id = intval($assoc['psl_id']);


            $data['invoice_no'] = htmlspecialchars(trim($this->input->post('invoice_no')));
            $sl_id = $this->shelves_model->get_shelves_id($data['invoice_no']);
            !$sl_id && $this->sys_message->wrong('货架位不存在!');

            $data['storage_number'] = intval($this->input->post('storage_number'));
            $data['storage_number'] <= 0 && $this->sys_message->wrong('请填写入库数量!');

            $data['storage_id'] = $this->purchase_goods_model->purchase_storage($data);
            !$data['storage_id'] && $this->sys_message->wrong('参数错误!');

            $pg_number_arr = $this->purchase_goods_model->allot_pg_number($data, $psl_id);
            
            if ($pg_number_arr['pg_ids']) {
                foreach ($pg_number_arr['pg_ids'] as $pg_id => $row) {
        
                    $log_info = "#{$pg_id} 采购产品入库 <b style='color: #ab2134'>{$row['need_number']}</b> 个";

                    $this->purchase_order_model->check_status($row['po_id'], 3, $log_info);
                }
            }
            $order_number_arr = $this->order_goods_model->allot_pg_number($data);

            if ($order_number_arr['rec_ids']) {
                foreach ($order_number_arr['rec_ids'] as $rec_id => $row) {

                    $log_info = "#{$rec_id} 订单产品占用采购入库 <b style='color: #ab2134'>{$row['need_number']}</b> 个库存";
                    $this->order_model->check_orders($row['order_id'], 2, $log_info);
                }
            }

            $data_shelves_sku = array(
                'sku_id' =>$data['sku_id'],
                'sl_id' => $sl_id,
                'sl_title' => $data['invoice_no'],
            );
            $this->shelves_model->set_shelves_sku($data_shelves_sku);

            $this->goods_sku_model->take_sku($data['sku_id'], 1, $data['storage_number'], $order_number_arr['occupy_number'], "#{$data['storage_id']} 采购产品入库", $data['storage_id']);

            $this->sys_message->success('成功!');
        } else {
            $this->sys_message->wrong('请填写运单号!');
        }

    }

    public function cc() {
        $this->load->model('order_goods_model');

        print_r($this->order_goods_model->get_need_inventory_goods(877));
    }
    public function quick_add_inventory_purchase()
    {
        $this->load->model('purchase_goods_model');
        $this->load->model('purchase_order_model');

        $this->load->library('sys_message');

        $assoc = $this->uri->uri_to_assoc();

        $pg_id = intval($assoc['pg_id']);

        if ($_POST) {

            $pg_number = htmlspecialchars(trim($this->input->post('pg_number')));
            !$pg_number && $this->sys_message->wrong('请填写数量!');

            $log_info = htmlspecialchars(trim($this->input->post('log_info')));

            $purchase_goods = $this->purchase_goods_model->get_id_goods_info(array('pg.pg_id' => $pg_id));
            !$purchase_goods && $this->sys_message->wrong('参数错误!');

            $purchase_goods['pg_status'] == 3 && $this->sys_message->wrong('采购已经完成,无法添加数量!');

            $data = array(
                'pg_id' => $purchase_goods['pg_id'],
                'sku_id' => $purchase_goods['sku_id'],
                'goods_id' => $purchase_goods['goods_id'],
                'supp_id' => $purchase_goods['supp_id'],
                'supp_url' => $purchase_goods['supp_url'],
                'add_number' => $pg_number,
                'purc_price' => $purchase_goods['in_price'] ? $purchase_goods['in_price'] : 0.00,
                'log_info' => $log_info,
                'add_time' => time()
            );
            $this->purchase_goods_model->add_goods($data);
            if ($purchase_goods['pg_status'] == 2) {
                $this->purchase_goods_model->add_goods_sku_supp(array(
                    'sku_id' => $purchase_goods['sku_id'],
                    'supp_id' => intval($purchase_goods['supp_id']),
                    'purchase_number' => $pg_number
                ));
            }

            $this->sys_message->success('成功!');

        } else {
            echo '<input id="pg_number" placeholder="输入数量" class="swal2-input"><input id="log_info" placeholder="输入备注" class="swal2-input">';
            exit;
        }
    }


    public function ajax_add_ali_no()
    {
        $this->load->model('purchase_order_model');

        $this->load->library('sys_message');

        $po_id = intval($this->input->post('po_id'));
        $ali_invoice_no = htmlspecialchars(trim($this->input->post('ali_invoice_no')));

        !$ali_invoice_no && $this->sys_message->wrong('请填阿里单号!');

        $this->purchase_order_model->update_order($po_id, array('ali_invoice_no' => $ali_invoice_no));

        $this->sys_message->success('成功!');
    }
    public function add_order_purchase_goods()
    {
        $this->load->model('order_goods_model');
        $this->load->model('purchase_goods_model');
        $this->load->model('purchase_order_model');

        $order_goods = $this->order_goods_model->not_purchase_order_goods();

        if ($order_goods) {
            $this->purchase_goods_model->purchase_order_goods($order_goods);
        }
        $purchase_goods = $this->purchase_goods_model->unallocated_goods_supp();
        if ($purchase_goods) {
            $this->purchase_order_model->batch_create_order($purchase_goods);
        }
    }
    public function ajax_sales()
    {
        $this->load->model('goods_sku_model');
        $this->load->model('order_goods_model');
        $this->load->model('purchase_goods_model');

        $pg_id = intval($this->input->get('pg_id'));

        $purchase_goods = $this->purchase_goods_model->goods_info(array('pg_id' => $pg_id));

        $times = array();

        $today = strtotime('today');
        $times['total'] = 0;
        $times['3day'] = $today - 86400 * 3;
        $times['today'] = $today;

        $data_name = array('total' => '至今', 'today' => '今日', '3day' => '近三天');
        $sum = array();
        foreach ($times as $k => $time) {
            $sum[$k]['name'] = $data_name[$k];
            $sum[$k]['order'] = $this->order_goods_model->get_order_goods_sales($purchase_goods['sku_id'], array('start' => $time));
            $sum[$k]['purchase'] = $this->purchase_goods_model->get_purchase_goods_sales($purchase_goods['sku_id'], array('start' => $time));
            $sum[$k]['inv_purchase'] = $this->purchase_goods_model->get_purchase_goods_sales($purchase_goods['sku_id'], array('start' => $time), 1);
            $sum[$k]['storage'] = $this->purchase_goods_model->get_purchase_storage_sales($purchase_goods['sku_id'], array('start' => $time));
        }

        $goods_sku = $this->goods_sku_model->sku_info(array('sku_id' => $purchase_goods['sku_id']));

        $data = array('sum' => $sum, 'goods_sku' => $goods_sku);
        $this->load->view('library/purchase_goods_sales', $data);

    }

    /* public function in_storage()
    {

        $this->load->model('purchase_goods_model');
        $this->load->model('goods_sku_model');
        $this->load->model('order_goods_model');

        $assoc = $this->uri->uri_to_assoc();

        $pg_id = intval($assoc['pg_id']);
        $sus_number = intval($this->input->post('sus_number'));

        $purchase_goods = $this->purchase_goods_model->goods_info(array('pg_id' => $pg_id, 'pg_status' => 2));


        if ($purchase_goods) {
            $data = array('pg_status' => 3);
            if ($sus_number) {
                $data['sus_number'] = $sus_number;
                $data['inv_number'] = $purchase_goods['pg_number'] - $sus_number;
            } else {
                $data['inv_number'] = $purchase_goods['pg_number'];
            }

            //获取需要获取分配的订单产品
            $order_goods = $this->order_goods_model->get_need_inventory_goods($purchase_goods['sku_id']);

            //分配采购产品
            $pg_number_arr = $this->purchase_goods_model->allot_pg_number($order_goods, array('inv_number' => $data['inv_number'], 'pg_id' => $purchase_goods['pg_id']));


            //占用库存写入sku,剩余采购产品则入库
            $this->goods_sku_model->take_sku($purchase_goods['sku_id'], 1, $data['inv_number'], $pg_number_arr['occupy_number'], '采购产品入库');

            //更改状态
            $this->purchase_goods_model->set_pg_status($pg_id, $data);

            $result = array('info' => '成功!', 'status' => 'success');
        } else {
            $result = array('info' => '批次产品不存在!', 'status' => 'error');
        }


        $is_ajax = $this->input->get_post('is_ajax');
        if ($is_ajax) {

            die(json_encode($result));
        }

    }*/

    public function up_ez_goods_sku(){
        // $this->load->library('Eccang');
        // $productSku = '1394-Green-S';

        // $this->eccang->syncProduct_edit($productSku);

        $_param_option = array(
            'goods_id' => array(
                'col' => 'col-lg-4',
                'type' => 'text',
                'placeholder' => '商品ID搜索'
            ),
            // 'pg_status' => array(
            //     'col' => 'col-lg-2',
            //     'type' => 'option',
            //     'placeholder' => '选择状态',
            //     'option' => $purchase_goods['pg_status']WY0284 WY0283
            // )
        );
        $_goods_list = [];
        $goods_id = $this->input->get('goods_id');
        if($goods_id){
            $_param['goods_id'] = $goods_id;
            $this->load->model('goods_sku_model');
            $_goods_list = $this->goods_sku_model->goods_sku_up($goods_id);
        }
        


        $data = array(
            'goods_list' => $_goods_list,
            'param_option' => $_param_option,
            'param' => $_param
        );
        $this->load->view('up_ez_goods_sku', $data);
    }

    //下架E站商品 下架本地商品 下架shopify商品
    public function goods_up_sku(){
        $this->load->library('Eccang');
        $this->load->model('goods_sku_model');
        $this->load->model('siteweb_model');
        $this->load->model('goods_model');
        $this->load->model('shopify_goods_model');
        $this->load->library('sys_message');
        $id = $this->input->post('id');
        $res = $this->goods_sku_model->sku_select($id);
        
        if($res['res'] == 1){
            //E站上架
            $this->eccang->syncProduct_edit($res['sku_sn'],2);
            $this->load->library('shopify_api');
            //shopify 上架
            //不管上架商品含是sku覆盖上架
            $arr = $this->goods_sku_model->suk_goods_id($id);
                foreach ($arr as $k => $v) {
                    $web_info = $this->siteweb_model->siteweb_info(array('web_id' => $v['web_id']), 1);
                    $goods = $this->goods_model->goods_info($v['goods_id'], 1);
                    $sku_query = $this->db->from('goods_sku')->select('sku_num_sn')
                                      ->where(['goods_id'=>$v['goods_id'],'is_on_sale'=>1])->get();
                    $post_sku = $sku_query->result_array();
                    // PRINT_r($post_sku);die;
                    $data = array();
                    foreach ($post_sku as $num_sn) {
                        $_goods_sku = $goods['sku'][$num_sn['sku_num_sn']];
                        if ($_goods_sku) {
                            $data['variants'][] = array(
                                'option1' => $_goods_sku['size'],
                                'option2' => $_goods_sku['color'],
                                'sku' => $web_info['ab_name'] . $num_sn['sku_num_sn'],
                                'price' => $goods['shop_price']
                            );
                        }
                    }

                    $this->shopify_api->initialize($web_info);
                    $data['shopify_id'] = $v['shopify_id'];
                    $response = $this->shopify_api->update_is_sales($data);

                    $data['variants'] = serialize($data['variants']);
                    $this->db->where('sg_id', $v['sg_id']);
                    $this->db->update('shopify_goods', $data);
                }
        }if($res['res'] == 2){
            echo 1;die;
            //E站下架
            $this->eccang->syncProduct_edit($res['sku_sn'],1);
            $this->load->library('shopify_api');

            //shopify 下架
            if($res['data'] == '只下架sku'){
                //只下架sku覆盖商品
                $arr = $this->goods_sku_model->suk_goods_id($id);

                foreach ($arr as $k => $v) {
                    $web_info = $this->siteweb_model->siteweb_info(array('web_id' => $v['web_id']), 1);
                    $goods = $this->goods_model->goods_info($v['goods_id'], 1);
                    $sku_query = $this->db->from('goods_sku')->select('sku_num_sn')
                                      ->where(['goods_id'=>$v['goods_id'],'is_on_sale'=>1])->get();
                    $post_sku = $sku_query->result_array();
                    // PRINT_r($post_sku);die;
                    $data = array();
                    foreach ($post_sku as $num_sn) {
                        $_goods_sku = $goods['sku'][$num_sn['sku_num_sn']];
                        if ($_goods_sku) {
                            $data['variants'][] = array(
                                'option1' => $_goods_sku['size'],
                                'option2' => $_goods_sku['color'],
                                'sku' => $web_info['ab_name'] . $num_sn['sku_num_sn'],
                                'price' => $goods['shop_price']
                            );
                        }
                    }

                    $this->shopify_api->initialize($web_info);
                    $data['shopify_id'] = $v['shopify_id'];
                    $response = $this->shopify_api->update_is_sales($data);

                    $data['variants'] = serialize($data['variants']);
                    $this->db->where('sg_id', $v['sg_id']);
                    $this->db->update('shopify_goods', $data);
                    
                    
                }

            }elseif($res['data'] == '把产品和sku下架'){
                //把商品也下架
                $arr = $this->goods_sku_model->suk_goods_id($id);
                foreach ($arr as $k => $v) {
                    $siteweb = $this->siteweb_model->siteweb_info(array('web_id' => $v['web_id']));
                    $this->load->library('shopify_api', $siteweb);
                    $this->shopify_api->published_status_sales($v['shopify_id'],2);
                }
            }

        }
        $this->sys_message->success('成功!');

    }

    public function ecopen()
    {
        $this->load->library('Eccang');
        $pagecount = $this->input->get('pagecount');

        $on_sale_goods = $this->eccang->aa($pagecount);

        foreach ($on_sale_goods['data'] as $item) {
            $on_sku[] = $item['productSku'];
        }

        print_r($on_sku);
        exit;

        $this->db->set('is_on_sale', 0);
        $this->db->where_in('sku_sn', $on_sku);
        $this->db->update('goods_sku');

        $sql = "select distinct goods_id from pcs_goods_sku where sku_sn in ('" . implode("','", $on_sku) . "')";

        $arr_query = $this->db->from('goods_sku')->select('count(1) as sale_count, is_on_sale, goods_id')->where("goods_id in ($sql)")->group_by('goods_id,is_on_sale')->get();
        $arr = $arr_query->result_array();

        $goods = array();
        print_r($arr);
        foreach ($arr as $row) {
            $goods[$row['goods_id']] = $row['goods_id'];
            if ($row['is_on_sale'] == 1) {
                unset($goods[$row['goods_id']]);
            }
        }

        $this->db->set('is_on_sale', 0);
        $this->db->where_in('goods_id', $goods);
        $this->db->update('goods');
    }
    
    //下架商品
    public function goods_up(){
        $this->load->library('Eccang');
        $this->load->model('goods_sku_model');
        $this->load->model('siteweb_model');
        $this->load->model('goods_model');
        $this->load->model('shopify_goods_model');
        $this->load->library('sys_message');
        $goods_id = $this->input->post('goods_id');
        //下架商品和sku
        $this->goods_sku_model->up_goods($goods_id);
        //E站下架
        $res = $this->goods_sku_model->goods_sku_select($goods_id);
        foreach ($res as $k => $v) {
            $this->eccang->syncProduct_edit($v['sku_sn'],1);
        }
        //shopify 下架
        $arr = $this->goods_sku_model->suk_goods_id(0,$goods_id);
        foreach ($arr as $k => $v) {
            $siteweb = $this->siteweb_model->siteweb_info(array('web_id' => $v['web_id']));
            $this->load->library('shopify_api', $siteweb);
            $this->shopify_api->published_status_sales($v['shopify_id'],2);
        }
        $this->sys_message->success('成功!');
    }

    public function aa(){
        $pagecount = $this->input->get('pagecount');
        $this->load->library('Eccang');
        $productSku = '1394-Green-M';

        $ww = $this->eccang->aa($productSku,$pagecount);
        foreach ($ww['data'] as $k => $v) {
            $goods_id = $this->db->from('goods_sku')->select('goods_id')
                ->where(['sku_sn'=>$v['productSku']])->get();
            $goods_id = $goods_id->row_array();
            $ww['data'][$k]['goods_id'] = $goods_id['goods_id'];
        }
        $new = $this->second_array_unique_bykey($ww['data'],'goods_id');

        //array_unique($arr)
        // print_R($new);die;
        $goods_id = [];
        foreach ($new as $k => $v) {
            $goods_id[] = $v['goods_id'];
        }
        $this->db->select('count(*) count,sku_id,goods_id');
        $this->db->group_by("goods_id");
        $this->db->from('goods_sku');
        $this->db->where_in('goods_id', $goods_id);
        $query = $this->db->get();
        $aa = $query->result_array();

        foreach ($aa as $key => $value) {
            $i = 0;
            foreach ($ww['data'] as $k => $val) {
                if($value['goods_id'] == $val['goods_id']){
                    $i = $i + 1;
                }
                //下架sku
                $data = ['is_on_sale'=>0];
                $this->db->where('goods_id', $value['goods_id']);
                $ar = $this->db->update('goods_sku', $data);
            }
            if($i == $value['count']){
                //修改商品下架
                $data = ['is_on_sale'=>0];
                $this->db->where('goods_id', $value['goods_id']);
                $this->db->update('goods', $data);
                
            }
        }

        echo '孙坚大Py子';
    }


    function second_array_unique_bykey($arr, $key){
        $tmp_arr = array();
        foreach($arr as $k => $v)
        {
        if(in_array($v[$key], $tmp_arr))  //搜索$v[$key]是否在$tmp_arr数组中存在，若存在返回true
        {
          unset($arr[$k]); //销毁一个变量 如果$tmp_arr中已存在相同的值就删除该值
        }else {
          $tmp_arr[$k] = $v[$key]; //将不同的值放在该数组中保存
        }
        }
        //ksort($arr); //ksort函数对数组进行排序(保留原键值key) sort为不保留key值
        return $arr;
  }




    // public function getProductList(){
    //     $this->load->library('Eccang');
    //     //$productSku = '1394-Green-S';

    //     $this->eccang->getProductList();
    // }
}
